﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OrderApp;

namespace OrderWeb.Controllers {
    [Route("[controller]")]
    [ApiController]
    public class OrderServiceController : ControllerBase {
        private readonly OrderService _orderService;

        public OrderServiceController(OrderService orderService) {
            _orderService = orderService;
        }

        [HttpGet("AllOrders")]
        public ActionResult<List<Order>> GetAllOrders() {
            return _orderService.Orders;
        }

        [HttpGet("QureyOrder/OrderId")]
        public ActionResult<Order> GetOrderById(string id) {
            return _orderService.GetOrder(id);
        }


        [HttpGet("QureyOrder/GoodsName")]
        public List<Order> GetOrderByGoodName(string goodName) {
            return _orderService.QueryOrdersByGoodsName(goodName);
        }


        [HttpGet("QureyOrder/CustomerName")]
        public ActionResult<List<Order>> GetOrderByCustomerName(string customerName) {
            return _orderService.QueryOrdersByCustomerName(customerName);
        }

        [HttpDelete("DeletOrder")]
        public string Delete(string id) {
            try {
                _orderService.RemoveOrder(id);
                return "Delete Success.";
            }
            catch (Exception e) {
                return $"Delete failed:{e.Message}";
            }
        }

        [HttpPut("AddOrder")]
        public string Put(Order order) {
            try {
                _orderService.UpdateOrder(order);
                return "Add success";
            }
            catch (Exception e) {
                return $"Add Failed: {e.Message}";
            }
        }

        [HttpGet("GetDetails")]
        public ActionResult<List<OrderDetail>> GetDetails(string id) {
            return _orderService.GetOrder(id).Details;
        }

        [HttpPut("AddDetail")]
        [HttpDelete("DeleteDetail")]
        public void PutDetails(Order order) {
            _orderService.UpdateOrder(order);
        }
    }

}
